import axios from '@ohos/axios'
import Account from '../viewModel/Account'
import User from '../viewModel/User'

// let baseUrl= '10.2.2.108'
let baseUrl= '192.168.43.254'
class UserModel{
  Login(username:string,password:string):Promise<User>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'POST',
        // url:'http://localhost:8080/user/login',
        // url:'http://10.67.64.19:8080/user/login',
        url:'http://'+baseUrl+':8080/user/login',
        data:{
          username:username,
          password:password
        },
        headers:{
          'Content-Type': 'application/json',
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('登录失败')
      })
    })
  }
  Register(user:User):Promise<number>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'POST',
        // url:'http://localhost:8080/user/register',
        url:'http://'+baseUrl+':8080/user/register',
        data:{
          username:user.username,
          password:user.password
        },
        headers:{
          'Content-Type': 'application/json',
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('注册失败')
      })
    })
  }

  UpdateUser(user:User):Promise<number>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'POST',
        // url:'http://localhost:8080/user/updateUser',
        url:'http://'+baseUrl+':8080/user/updateUser',
        data:{
          uid:user.uid,
          email:user.email,
          phone:user.phone
        },
        headers:{
          'Content-Type': 'application/json',
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('更新失败')
      })
    })
  }
}
const userModel = new UserModel();
export default userModel as UserModel;
